package exp7;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class PhotoWindows extends JFrame {
	PhotoWindows(String title) {
		setLayout(null);
		setTitle(title);
		setSize(1300, 1000);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
	}

	void showMe() {
		setVisible(true);
	}
}

class ImageViewer {
	int s = 0;
	String[] a = { "images/0.png", "images/1.jpg", "images/2.jpg", "images/3.jpg", "images/4.jpg", "images/5.jpg", "images/6.jpg", "images/7.png", "images/8.png", "images/9.png", "images/10.jpg",
			"images/11.jpg", "images/12.jpg","images/13.jpg", "images/14.png", "images/15.png" ,"images/16.png" };

	JLabel[] lbs = new JLabel[17];
	JLabel lb = new JLabel();
	JButton b1 = new JButton();
	JButton b2 = new JButton();
	JPanel p = new JPanel();
	JScrollPane sp = new JScrollPane(p);

	void init() {
		PhotoWindows f = new PhotoWindows("图片查看器");
		ImageIcon image = new ImageIcon("images/0.png");
		image.setImage(image.getImage().getScaledInstance(1300, 700, Image.SCALE_DEFAULT));
		lb.setIcon(image);

		b1.setIcon(new ImageIcon("images/first.png"));

		b2.setIcon(new ImageIcon("images/last.png"));

		for (int i = 0; i < lbs.length; i++) {
			lbs[i] = new JLabel();
			ImageIcon images = new ImageIcon(a[i]);
			images.setImage(images.getImage().getScaledInstance(400, 200, Image.SCALE_DEFAULT));
			lbs[i].setIcon(images);
			p.add(lbs[i]);
		}
		lb.setSize(1500, 700);
		lb.setLocation(0, 0);
		f.add(lb);
		b1.setSize(100, 40);
		b1.setLocation(600, 700);
		b2.setSize(100, 40);
		b2.setLocation(700, 700);
		f.add(b1);
		f.add(b2);
		sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		sp.setSize(1300, 200);
		sp.setLocation(0, 750);
		f.add(sp);
		f.showMe();
		f.setIconImage(new ImageIcon("张.png").getImage());
		MyActionListener listener = new MyActionListener();

		b1.setActionCommand("last");
		b1.addActionListener(listener);
		b2.setActionCommand("next");
		b2.addActionListener(listener);

	}

	class MyActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			switch (e.getActionCommand()) {
				case "last":
					if (s == 0) {
						s = 16;
					} else {
						s--;
					}
					ImageIcon image1 = new ImageIcon(a[s]);
					image1.setImage(image1.getImage().getScaledInstance(1300, 700, Image.SCALE_DEFAULT));
					lb.setIcon(image1);

					break;
				case "next":
					if (s == 16) {
						s = 0;
					} else {
						s++;
					}
					ImageIcon image2 = new ImageIcon(a[s]);
					image2.setImage(image2.getImage().getScaledInstance(1300, 700, Image.SCALE_DEFAULT));
					lb.setIcon(image2);
					break;
			}

		}

	}

}

public class Exp7_3 {
	public static void main(String[] args) {
		new ImageViewer().init();
	}
}
